package com.mr.base.alpha.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mr.base.alpha.dos.UserDO;
import com.mr.base.alpha.mappers.expand.UserExpandMapper;
import com.mr.base.alpha.params.UserParam;
import com.mr.base.alpha.service.UserService;
import com.mr.base.core.common.exception.BaseException;
import com.mr.base.core.common.idworker.SnowFlake;
import com.mr.base.core.common.vo.CodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserExpandMapper userExpandMapper;

    @Autowired
    private SnowFlake snowFlake;

    @Override
    public CodeVO createUser(UserDO userDO) throws BaseException {
        Long id = snowFlake.nextId();
        userDO.setId(id);

        userExpandMapper.insertSelective(userDO);

        return CodeVO.of(id);
    }

    @Override
    public PageInfo<UserDO> pageUser(UserParam param) throws BaseException {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());


        final PageInfo<UserDO> sourcePage = new PageInfo<UserDO>(userExpandMapper.listUser(param));

        return sourcePage;
    }
}
